package cn.edu.bzu.manager.computer.service;

import cn.edu.bzu.manager.computer.dao.admin.AdminUserDAO;
import cn.edu.bzu.manager.computer.entity.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员业务逻辑层
 *
 * @author 1923111021王云飞
 * @since 1.8
 */

@Service
public class AdminService {

    private AdminUserDAO adminUserDAO;

    @Autowired
    public void setAdminUserDAO(AdminUserDAO adminUserDAO) {
        this.adminUserDAO = adminUserDAO;
    }

    /**
     * 通过名字和密码查找管理员
     * 可用于验证登录信息
     *
     * @param name     名字
     * @param password 密码
     * @return 查找到的管理员对象
     */
    public AdminUser findAdminUserByNameAndPassword(String name, String password) {
        List<AdminUser> users = adminUserDAO.findByNameAndPassword(name, password);
        if (!users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    /**
     * 查找所有管理员的ID和名字
     *
     * @return ID和名字K-V(Map)集合
     */
    public List<Map<String, Object>> findAllAdminIdAndName() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AdminUser user : adminUserDAO.findAll()) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", user.getId());
            map.put("name", user.getName());
            list.add(map);
        }
        return list;
    }

}
